import {Size, SpriteFrame, Vec2} from "cc";
import Long from "long";
import {configsHelper} from "db://assets/script/configs-helper";
import {propsConfigsLoader} from "db://assets/script/configs/props-configs-loader";

export namespace cityUtils {

    export function calcBuildingScaleInWorld(buildingSize: Size): Size {
        const r = 1 + (buildingSize.x + buildingSize.y - 2) * 0.5
        return new Size(r, r)
    }

    export function calcBuildingIcon(buildingId: Long): SpriteFrame | null {
        const bc = configsHelper.loadBuildingConfig(buildingId)
        return propsConfigsLoader.findPropIcon(bc.propId.toNumber())
    }

    export function calcTilePositionByCellId(value: bigint): Vec2 {
        // 创建64位全1掩码 (0xFFFFFFFFFFFFFFFF)
        const mask64 = (1n << 64n) - 1n;

        // 确保只处理64位范围内的值
        const safeValue = value & mask64;

        // 提取低32位
        const lowBits = Number(safeValue & 0xFFFFFFFFn);

        // 提取高32位
        const highBits = Number((safeValue >> 32n) & 0xFFFFFFFFn);

        // 转换为32位有符号整数
        return new Vec2(highBits | 0, lowBits | 0);
    }

    export function calcCellId(x: number, y: number): bigint {
        // 将高32位转换为无符号整数后左移32位
        const highPart = BigInt(x >>> 0) << 32n;
        // 将低32位转换为无符号整数
        const lowPart = BigInt(y >>> 0);
        // 组合高低位
        return highPart | lowPart;
    }

    export function calcBuildingOccupiedCellIds(x: number, y: number, buildingId: Long): bigint[] {
        const config = configsHelper.loadBuildingConfig(buildingId)!
        const buildingSize = new Size(config.height, config.width)
        let rst: Array<bigint> = []
        console.log('init building: building', buildingId.toString(), 'x', x, 'y', y)
        for (let i = 0; i < buildingSize.height; i++) {
            for (let j = 0; j < buildingSize.width; j++) {
                rst.push(calcCellId(x - j, y - i))
            }
        }
        return rst
    }


    /**
     * 逐行对角线遍历 - 最直观的方法
     * @param width 地图宽度
     * @param height 地图高度
     * @returns 遍历的单元格坐标数组
     */
    export function rowByRowDiagonalTraverse(width: number, height: number): { x: number, y: number }[] {
        const result: { x: number, y: number }[] = [];

        // 遍历每一行
        for (let row = 0; row < height; row++) {
            // 遍历当前行的对角线
            for (let x = 0, y = row; x < width && y >= 0; x++, y--) {
                result.push({x, y});
            }
        }

        // 遍历右侧剩余的对角线
        for (let col = 1; col < width; col++) {
            for (let x = col, y = height - 1; x < width && y >= 0; x++, y--) {
                result.push({x, y});
            }
        }

        return result;
    }

    /**
     * 通用对角线索引计算公式
     * @param x 横坐标
     * @param y 纵坐标
     * @param width 地图宽度
     * @param height 地图高度
     * @returns 遍历顺序索引
     */
    export function universalDiagonalIndex(x: number, y: number, width: number, height: number): number {
        const d = x + y;

        if (d < Math.min(width, height)) {
            // 上半三角形区域
            return (d * (d + 1)) / 2 + x;
        } else if (d < Math.max(width, height)) {
            // 中间平行四边形区域
            const minDim = Math.min(width, height);
            return (minDim * (minDim + 1)) / 2 + (d - minDim) * minDim + x;
        } else {
            // 下半三角形区域
            const triangleBase = width + height - d - 1;
            return width * height - (triangleBase * (triangleBase + 1)) / 2 - (width - x);
        }
    }


}